using Microsoft.EntityFrameworkCore;
using server.Models;

namespace server.Services;

public class LibraryContext:DbContext
{
    public DbSet<Book> Books => Set<Book>();
    public DbSet<BorrowRecord> BorrowRecords =>Set<BorrowRecord>();
    public DbSet<User> Users =>Set<User>();
    public DbSet<Role> Roles =>Set<Role>();
    public DbSet<UserToken> Tokens =>Set<UserToken>();
    public LibraryContext()
    {
        
    }
    public LibraryContext(DbContextOptions<LibraryContext> options):base(options)
    {
        
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        optionsBuilder.UseSqlite("Data Source=library.db");
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Book>()
            .Property(b=>b.InDate)
            .HasDefaultValueSql("date('now')");
    }
    public async Task ImportBooks(IEnumerable<Book> books){
        await Books.AddRangeAsync(books);
        await SaveChangesAsync();
    }

}